package com.minhua.jijiehao2.mainframe.common.shareactivity.impl

import android.text.TextUtils
import android.view.View
import android.widget.EditText
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.bean.ShareInfoBean
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.mainframe.common.shareactivity.imp.IShareView
import com.minhua.jijiehao2.mainframe.main.impl.MainActivity
import com.minhua.jijiehao2.utils.SPUtils
import com.minhua.jijiehao2.utils.ShareSdkUtils
import com.minhua.jijiehao2.view.CustomDialog
import kotlinx.android.synthetic.main.activity_share.*

class ShareActivity : BaseActivity<IShareView, SharePresenter>(), IShareView {

    override fun getLayoutId(): Int = R.layout.activity_share

    override fun getPresenter(): SharePresenter = SharePresenter()

    private val mOnClickListener = View.OnClickListener { v ->
        when (v.id) {
            R.id.tvShareRule -> ruleDialog?.show()
            R.id.tvShare -> mPresenter.getShareInfo()
            R.id.tvShareCreditCard -> MainActivity.intentStart(this, 3)
            R.id.tvShareLoan -> MainActivity.intentStart(this, 1)
            R.id.tvSubmit -> {
                addressDialog?.dismiss()
                mPresenter.setAddress()
            }
        }
    }

    private var ruleDialog: CustomDialog? = null
    private var addressDialog: CustomDialog? = null
    private var shareSuccess: CustomDialog? = null

    override fun initView() {
        tvShareRule.setOnClickListener(mOnClickListener)
        tvShare.setOnClickListener(mOnClickListener)
        tvShareCreditCard.setOnClickListener(mOnClickListener)
        tvShareLoan.setOnClickListener(mOnClickListener)
        ruleDialog = CustomDialog(this, R.layout.layout_dialog_share_rule)
        addressDialog = CustomDialog(this, R.layout.layout_dialog_share_address, false)
        addressDialog?.viewHolder?.setOnClickListener(R.id.tvSubmit, mOnClickListener)
        val nickname = SPUtils.get(DefineKey.USER_NICK_NAME, "") as String
        val phone = SPUtils.get(DefineKey.USER_NAME, "") as String
        if (!TextUtils.isEmpty(nickname)) {
            addressDialog?.viewHolder?.setText(R.id.tvName, "hi ~ " + nickname)
        } else {
            addressDialog?.viewHolder?.setText(R.id.tvName, "hi ~ " + phone)

        }
        shareSuccess = CustomDialog(this, R.layout.layout_dialog_share_success)
    }


    override fun onShareInfoSuccess(shareInfoBean: ShareInfoBean) {
        ShareSdkUtils.showNetShare(this, shareInfoBean, object : ShareSdkUtils.OnShareListener {
            override fun onSuccess() {
                addressDialog?.show()
            }
        })
    }

    override fun getName(): String = addressDialog?.viewHolder?.getView<EditText>(R.id.etName)?.text.toString().trim()

    override fun getTel(): String = addressDialog?.viewHolder?.getView<EditText>(R.id.etTel)?.text.toString().trim()

    override fun getAddress(): String = addressDialog?.viewHolder?.getView<EditText>(R.id.etAddress)?.text.toString().trim()

    override fun onAddressSuccess() {
        shareSuccess?.show()
    }
}
